package com.itheima.springboottilasmanager.controller;

import com.itheima.springboottilasmanager.pojo.domain.Result;
import com.itheima.springboottilasmanager.pojo.domain.Emp;
import com.itheima.springboottilasmanager.pojo.vo.LoginVO;
import com.itheima.springboottilasmanager.survice.EmpService;
import com.itheima.springboottilasmanager.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {
@Autowired
private EmpService empService;

    @PostMapping
    public Result login(@RequestBody Emp emp) {
        log.info("用户登录：{}", emp);
     Emp e =  empService.login(emp);
        LoginVO logVO = new LoginVO();
        BeanUtils.copyProperties(e, logVO);
//        使用JJWT生成令牌
//        4.1创建载荷，自定义数据
        Map claims = new HashMap<>();
//        4.2生成令牌
        claims.put("id",e.getId());
        claims.put("username",e.getUsername());
        claims.put("name",e.getName());
        claims.put("password",e.getPassword());
        String token = JwtUtils.generateJwt(claims);
        logVO.setToken(token);
        return Result.success(logVO);
    }
}
